package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import net.miginfocom.swing.MigLayout;


public class Premesse extends JPanel {

   private JPanel descr = new JPanel();

   private JTextPane descrizioneField = new JTextPane();

   private JPanel photo = new JPanel();
   private JButton upload1 = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
   private JButton upload2 = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
   private JButton upload3 = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
   private JButton upload4 = new JButton(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));

   private JPanel dati = new JPanel();
   private JTextField tempEsterno = new JTextField() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private JTextField tempInterno = new JTextField() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private JTextField tempInterrato = new JTextField() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private JTextField umidEsterno = new JTextField() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private JTextField umidInterno = new JTextField() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private JTextField umidInterrato = new JTextField() {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private JLabel umAssEsterno = new JLabel("   calcola") {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private JLabel umAssInterno = new JLabel("   calcola") {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private JLabel umAssInterrato = new JLabel("   calcola") {
      @Override
      protected void paintComponent(Graphics g) {
         if (!isOpaque()) {
            int w = getWidth();
            int h = getHeight();
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor(UIManager.getColor("TextField.background"));
            g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.setColor(Color.BLACK);
            g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
            g2.dispose();
         }
         super.paintComponent(g);
      }
   };
   private double[] tabella = { 3.24, 3.52, 3.81, 4.13, 4.47, 4.84, 5.20, 5.6, 6, 6.4, 6.8, 7.3, 7.8, 8.3, 8.8, 9.4, 10, 10.7, 11.4, 12.1, 12.8,
         13.6, 14.5, 15.4, 16.3, 17.3, 18.3, 19.4, 20.6, 21.80, 23, 24.4, 25.8, 27.2, 28.7, 30.3 };

   private final double vapore = 3.06;
   private final double kelvin = 273.15;
   private final double costante = 216.4;

   private JTextPane condizioniMeteo = new JTextPane();

   private String lastPath = "";
   private String pathIm1 = "";
   private String pathIm2 = "";
   private String pathIm3 = "";
   private String pathIm4 = "";
   private String sourceIm1 = "";
   private String sourceIm2 = "";
   private String sourceIm3 = "";
   private String sourceIm4 = "";

   private StartingWindow st;

   String directoryProject;

   protected void paintComponent(Graphics g) {
      setOpaque(false);
      g.drawImage((new ImageIcon(ClassLoader.getSystemResource("sfondo.jpg"))).getImage(), 0, 0, null);
      super.paintComponent(g);
   }

   public Premesse(final StartingWindow st) {
      super();
      this.st = st;
      setLayout(new MigLayout("", "", "25[]"));
      Properties pc = System.getProperties();
      String homePath = pc.getProperty("user.home");
      directoryProject = homePath + pc.getProperty("file.separator") + "ArchimedeSuper" + pc.getProperty("file.separator") + "aaAAzzZZ"
            + pc.getProperty("file.separator");

      descr.setLayout(new MigLayout());
      descr.setOpaque(false);
      JLabel descrittiva = new JLabel("Premessa descrittiva : Indicare obbiettivi e desideri del committente");
      descrittiva.setFont(new Font("Arial", Font.BOLD, 20));
      descrizioneField.setBorder(BorderFactory.createLineBorder(Color.GRAY));
      descrizioneField.setPreferredSize(new Dimension(700, 200));
      descrizioneField.setFont(new Font("Arial", Font.PLAIN, 16));
      descr.add(descrittiva, "wrap");
      descr.add(descrizioneField, "wrap");

      dati.setLayout(new MigLayout("", "[]1[]1[]", "[]5[] 7[] 7[] 7[]"));
      dati.setOpaque(false);
      setCellClima();

      photo.setLayout(new MigLayout());
      photo.setOpaque(false);
      JLabel infoUpload = new JLabel("Inserisci le tue foto : ");
      infoUpload.setFont(new Font("Arial", Font.BOLD, 20));

      upload1.setBorder(null);
      upload1.setContentAreaFilled(false);
      upload1.setRolloverEnabled(true);
      upload1.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
      upload1.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            // filechooser
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            ImagePreviewPanel preview = new ImagePreviewPanel();
            chooser.setAccessory(preview);
            chooser.addPropertyChangeListener(preview);
            lastPath = st.getPath();
            if (lastPath.equals(""))
               chooser.getCurrentDirectory();
            else
               chooser.setCurrentDirectory(new File(lastPath));
            int returnValue = chooser.showOpenDialog(upload1);

            if (returnValue == JFileChooser.APPROVE_OPTION) {
               String sourcePath = chooser.getSelectedFile().toString();

               st.setPath(sourcePath);
               lastPath = sourcePath;

               StringTokenizer tok = new StringTokenizer(sourcePath, ".");
               String sufix = "";
               while (tok.hasMoreTokens())
                  sufix = tok.nextToken();

               sourceIm1 = sourcePath;
               ImageIcon tmp = new ImageIcon(sourcePath);
               tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

               String destPath = directoryProject + "photo1Premesse." + "jpg";
               pathIm1 = destPath;

               upload1.setIcon(tmp);

               photo.add(upload2, "cell 1 0");
               st.refresh();
            }
         }
      });

      upload2.setBorder(null);
      upload2.setContentAreaFilled(false);
      upload2.setRolloverEnabled(true);
      upload2.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
      upload2.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            ImagePreviewPanel preview = new ImagePreviewPanel();
            chooser.setAccessory(preview);
            chooser.addPropertyChangeListener(preview);
            lastPath = st.getPath();
            if (lastPath.equals(""))
               chooser.getCurrentDirectory();
            else
               chooser.setCurrentDirectory(new File(lastPath));
            int returnValue = chooser.showOpenDialog(upload2);

            if (returnValue == JFileChooser.APPROVE_OPTION) {
               String sourcePath = chooser.getSelectedFile().toString();
               st.setPath(sourcePath);
               lastPath = sourcePath;

               sourceIm2 = sourcePath;
               ImageIcon tmp = new ImageIcon(sourcePath);
               tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

               String destPath = directoryProject + "photo2Premesse." + "jpg";
               pathIm2 = destPath;

               upload2.setIcon(tmp);

               photo.add(upload3, "cell 2 0");

               st.refresh();
            }
         }
      });

      upload3.setBorder(null);
      upload3.setContentAreaFilled(false);
      upload3.setRolloverEnabled(true);
      upload3.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
      upload3.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {

            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            ImagePreviewPanel preview = new ImagePreviewPanel();
            chooser.setAccessory(preview);
            chooser.addPropertyChangeListener(preview);
            lastPath = st.getPath();
            if (lastPath.equals(""))
               chooser.getCurrentDirectory();
            else
               chooser.setCurrentDirectory(new File(lastPath));
            int returnValue = chooser.showOpenDialog(upload3);

            if (returnValue == JFileChooser.APPROVE_OPTION) {
               String sourcePath = chooser.getSelectedFile().toString();
               st.setPath(sourcePath);
               lastPath = sourcePath;

               sourceIm3 = sourcePath;
               ImageIcon tmp = new ImageIcon(sourcePath);
               tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

               String destPath = directoryProject + "photo3Premesse." + "jpg";
               pathIm3 = destPath;
               upload3.setIcon(tmp);
               photo.add(upload4, "cell 3 0");

               st.refresh();
            }
         }
      });

      upload4.setBorder(null);
      upload4.setContentAreaFilled(false);
      upload4.setRolloverEnabled(true);
      upload4.setRolloverIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon.jpg")));
      upload4.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            ImagePreviewPanel preview = new ImagePreviewPanel();
            chooser.setAccessory(preview);
            chooser.addPropertyChangeListener(preview);
            lastPath = st.getPath();
            if (lastPath.equals(""))
               chooser.getCurrentDirectory();
            else
               chooser.setCurrentDirectory(new File(lastPath));
            int returnValue = chooser.showOpenDialog(upload4);

            if (returnValue == JFileChooser.APPROVE_OPTION) {
               String sourcePath = chooser.getSelectedFile().toString();
               st.setPath(sourcePath);
               lastPath = sourcePath;

               sourceIm4 = sourcePath;
               ImageIcon tmp = new ImageIcon(sourcePath);
               tmp.setImage(tmp.getImage().getScaledInstance(250, 200, Image.SCALE_AREA_AVERAGING));

               String destPath = directoryProject + "photo4Premesse." + "jpg";
               pathIm4 = destPath;

               upload4.setIcon(tmp);

               st.refresh();
            }
         }
      });

      MouseAdapter ml1 = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
               int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
               if (res == 0) {
                  if (!pathIm2.equals("")) {
                     // scalo i valori dell'immagine 2 nell'immagine 1
                     pathIm1 = pathIm2.replace("2P", "1P");

                     sourceIm1 = sourceIm2;
                     upload1.setIcon(upload2.getIcon());

                     if (!pathIm3.equals("")) {
                        // scalo i valori dell'immagine 3 nell'immagine 2
                        pathIm2 = pathIm3.replace("3P", "2P");

                        sourceIm2 = sourceIm3;
                        upload2.setIcon(upload3.getIcon());
                        if (!pathIm4.equals("")) {
                           // scalo i valori dell'immagine 4 nell'immagine 3
                           pathIm3 = pathIm4.replace("4P", "3P");

                           sourceIm3 = sourceIm4;
                           // reset immagine 4
                           pathIm4 = "";

                           sourceIm4 = "";
                           upload3.setIcon(upload4.getIcon());
                           upload4.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                        } else {
                           // reset immagine 3
                           upload3.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                           pathIm3 = "";

                           sourceIm3 = "";
                           try {
                              photo.remove(upload4);
                           } catch (Exception noRemove) {
                              System.out.println("no remove");
                           }

                        }
                     } else {
                        // reset immagine 2
                        upload2.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                        pathIm2 = "";

                        sourceIm2 = "";
                        try {
                           photo.remove(upload3);
                        } catch (Exception noRemove) {
                           System.out.println("no remove");
                        }

                     }
                  } else {
                     // reset immagine 1
                     pathIm1 = "";

                     sourceIm1 = "";
                     upload1.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                     try {
                        photo.remove(upload2);
                     } catch (Exception noRemove) {
                        System.out.println("no remove");
                     }
                  }
                  st.refresh();
               }
            }
         }
      };
      upload1.addMouseListener(ml1);

      MouseAdapter ml2 = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
               int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
               if (res == 0) {

                  if (!pathIm3.equals("")) {
                     pathIm2 = pathIm3.replace("3P", "2P");

                     sourceIm2 = sourceIm3;
                     upload2.setIcon(upload3.getIcon());
                     if (!pathIm4.equals("")) {
                        pathIm3 = pathIm4.replace("4P", "3P");

                        sourceIm3 = sourceIm4;
                        pathIm4 = "";

                        sourceIm4 = "";
                        upload3.setIcon(upload4.getIcon());
                        upload4.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                     } else {
                        upload3.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                        pathIm3 = "";

                        sourceIm3 = "";
                        try {
                           photo.remove(upload4);
                        } catch (Exception noRemove) {
                           System.out.println("no remove");
                        }
                     }
                  } else {
                     upload2.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                     pathIm2 = "";

                     sourceIm2 = "";
                     try {
                        photo.remove(upload3);
                     } catch (Exception noRemove) {
                        System.out.println("no remove");
                     }

                  }
                  st.refresh();
               }
            }
         }
      };
      upload2.addMouseListener(ml2);

      MouseAdapter ml3 = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
               int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
               if (res == 0) {

                  if (!pathIm4.equals("")) {
                     pathIm3 = pathIm4.replace("4P", "3P");
                     sourceIm3 = sourceIm4;
                     pathIm4 = "";
                     sourceIm4 = "";
                     upload3.setIcon(upload4.getIcon());
                     upload4.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                  } else {
                     upload3.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                     pathIm3 = "";
                     sourceIm3 = "";
                     try {
                        photo.remove(upload4);
                     } catch (Exception noRemove) {
                        System.out.println("no remove");
                     }
                  }
                  st.refresh();
               }

            }
         }
      };
      upload3.addMouseListener(ml3);

      MouseAdapter ml4 = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3) {
               int res = JOptionPane.showConfirmDialog(null, "Eliminare foto?", "Attenzione", 0);
               if (res == 0) {
                  upload4.setIcon(new ImageIcon(ClassLoader.getSystemResource("bupload.jpg")));
                  pathIm4 = "";
                  sourceIm4 = "";
                  st.refresh();
               }

            }
         }
      };
      upload4.addMouseListener(ml4);

      MouseAdapter formulaEsterno = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3 || e.getButton() == MouseEvent.BUTTON1) {
               try {
                  umAssEsterno.setText("" + formula(tempEsterno.getText(), umidEsterno.getText()));
                  paint(getGraphics());
               } catch (Exception err) {
                  System.out.print("error umidità");
               }
            }
         }
      };
      umAssEsterno.addMouseListener(formulaEsterno);

      MouseAdapter formulaInterrato = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3 || e.getButton() == MouseEvent.BUTTON1) {
               try {
                  umAssInterrato.setText("" + formula(tempInterrato.getText(), umidInterrato.getText()));
                  paint(getGraphics());
               } catch (Exception err) {
                  System.out.print("error umidità");
               }
            }
         }
      };
      umAssInterrato.addMouseListener(formulaInterrato);

      MouseAdapter formulaInterno = new MouseAdapter() {
         public void mousePressed(MouseEvent e) {
            if (e.getButton() == MouseEvent.BUTTON3 || e.getButton() == MouseEvent.BUTTON1) {
               try {
                  umAssInterno.setText("" + formula(tempInterno.getText(), umidInterno.getText()));
                  paint(getGraphics());
               } catch (Exception err) {
                  System.out.print("error umidità");
               }
            }
         }
      };
      umAssInterno.addMouseListener(formulaInterno);

      ClipboardPopupMenu cb = ClipboardPopupMenu.installForComponent(descrizioneField);
      cb.addCopyFunction("Copia");
      cb.addCutFunction("Taglia");
      cb.addPasteFunction("Incolla");

      ClipboardPopupMenu cbMeteo = ClipboardPopupMenu.installForComponent(condizioniMeteo);
      cbMeteo.addCopyFunction("Copia");
      cbMeteo.addCutFunction("Taglia");
      cbMeteo.addPasteFunction("Incolla");

      photo.add(infoUpload, "dock north");
      photo.add(upload1, "cell 0 0");

      add(descr, "gapleft 15 , wrap");
      add(dati, "gapleft 15 , wrap");
      add(photo, "gapleft 15 , wrap");
   }

   public void setCellClima() {

      JLabel title = new JLabel("Dati climatici ambientali");
      RoundedPanel climaAmbientale = new RoundedPanel("Clima ambientale");
      RoundedPanel temperatura = new RoundedPanel("Temperatura");
      RoundedPanel umiditRelativa = new RoundedPanel("Umidita' relativa");
      RoundedPanel umiditAss = new RoundedPanel("Umidita' assoluta");
      RoundedPanel climaEst = new RoundedPanel("Clima esterno edificio C.E.");
      RoundedPanel climaInterno = new RoundedPanel("Clima interno edificio C.I.");
      RoundedPanel climaInterr = new RoundedPanel("Clima interrato edificio C.C.");
      JLabel infoMeteo = new JLabel("<html>Condizioni meteo al momento della diagnostica e<br>periodo antecedente alla rilevazione:</html>");

      umAssEsterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      umAssInterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      umAssInterrato.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

      condizioniMeteo.setFont(new Font("Arial", Font.PLAIN, 16));
      infoMeteo.setFont(new Font("Arial", Font.BOLD, 20));
      title.setFont(new Font("Arial", Font.BOLD, 20));
      umAssEsterno.setFont(new Font("Arial", Font.PLAIN, 14));
      umAssInterno.setFont(new Font("Arial", Font.PLAIN, 14));
      umAssInterrato.setFont(new Font("Arial", Font.PLAIN, 14));

      climaAmbientale.setPreferredSize(new Dimension(250, 30));
      temperatura.setPreferredSize(new Dimension(170, 30));
      umiditRelativa.setPreferredSize(new Dimension(170, 30));
      umiditAss.setPreferredSize(new Dimension(170, 30));
      climaEst.setPreferredSize(new Dimension(250, 30));
      climaInterno.setPreferredSize(new Dimension(250, 30));
      climaInterr.setPreferredSize(new Dimension(250, 30));
      umAssEsterno.setPreferredSize(new Dimension(170, 30));
      umAssInterno.setPreferredSize(new Dimension(170, 30));
      umAssInterrato.setPreferredSize(new Dimension(170, 30));
      condizioniMeteo.setPreferredSize(new Dimension(700, 70));

      climaAmbientale.setOpaque(false);

      temperatura.setOpaque(false);

      umiditRelativa.setOpaque(false);

      umiditAss.setOpaque(false);

      climaEst.setOpaque(false);

      climaInterno.setOpaque(false);

      climaInterr.setOpaque(false);

      tempEsterno.setPreferredSize(new Dimension(170, 30));
      tempInterno.setPreferredSize(new Dimension(170, 30));
      tempInterrato.setPreferredSize(new Dimension(170, 30));
      umidEsterno.setPreferredSize(new Dimension(170, 30));
      umidInterno.setPreferredSize(new Dimension(170, 30));
      umidInterrato.setPreferredSize(new Dimension(170, 30));

      tempEsterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      tempInterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      tempInterrato.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      umidEsterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      umidInterno.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
      umidInterrato.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));

      tempEsterno.setOpaque(false);
      tempInterno.setOpaque(false);
      tempInterrato.setOpaque(false);
      umidEsterno.setOpaque(false);
      umidInterno.setOpaque(false);
      umidInterrato.setOpaque(false);

      dati.add(title, "dock north");
      dati.add(climaAmbientale, "cell 0 0");
      dati.add(temperatura, "cell 1 0");
      dati.add(umiditRelativa, "cell 2 0");
      dati.add(umiditAss, "cell 3 0");
      // dati.add(climaInterno, "cell 0 1");
      // dati.add(tempInterno, "cell 1 1");
      // dati.add(umidInterno, "cell 2 1");
      // dati.add(umAssInterno, "cell 3 1");
      // dati.add(climaEst, "cell 0 2");
      // dati.add(tempEsterno, "cell 1 2");
      // dati.add(umidEsterno, "cell 2 2");
      // dati.add(umAssEsterno, "cell 3 2");
      // dati.add(climaInterr, "cell 0 3");
      // dati.add(tempInterrato, "cell 1 3");
      // dati.add(umidInterrato, "cell 2 3");
      // dati.add(umAssInterrato, "cell 3 3 , wrap");

      dati.add(climaEst, "cell 0 1");
      dati.add(tempEsterno, "cell 1 1");
      dati.add(umidEsterno, "cell 2 1");
      dati.add(umAssEsterno, "cell 3 1");
      dati.add(climaInterr, "cell 0 2");
      dati.add(tempInterrato, "cell 1 2");
      dati.add(umidInterrato, "cell 2 2");
      dati.add(umAssInterrato, "cell 3 2");
      dati.add(climaInterno, "cell 0 3");
      dati.add(tempInterno, "cell 1 3");
      dati.add(umidInterno, "cell 2 3");
      dati.add(umAssInterno, "cell 3 3 , wrap");

      JPanel meteo = new JPanel();
      meteo.setLayout(new MigLayout());
      meteo.setOpaque(false);
      meteo.add(infoMeteo, "wrap");
      meteo.add(condizioniMeteo);

      dati.add(meteo, "span , wrap");

   }

   public String print() {
      String print = "";
      print = print + "\n#START\n#" + descrizioneField.getText() + "\n#STOP";
      print = print + "\n#" + tempEsterno.getText();
      print = print + "\n#" + umidEsterno.getText();
      print = print + "\n#" + tempInterno.getText();
      print = print + "\n#" + umidInterno.getText();
      print = print + "\n#" + tempInterrato.getText();
      print = print + "\n#" + umidInterrato.getText();
      print = print + "\n#" + umAssEsterno.getText();
      print = print + "\n#" + umAssInterno.getText();
      print = print + "\n#" + umAssInterrato.getText();
      print = print + "\n#" + condizioniMeteo.getText();
      print = print + "\n#meteoStop";

      print = print + "\n#" + pathIm1.replace("aaAAzzZZ", st.getProject());
      print = print + "\n#" + pathIm2.replace("aaAAzzZZ", st.getProject());
      print = print + "\n#" + pathIm3.replace("aaAAzzZZ", st.getProject());
      print = print + "\n#" + pathIm4.replace("aaAAzzZZ", st.getProject());
      print = print + "\n@@Premesse";
      return print;
   }

   public void setElement(String data) {
      Scanner c = new Scanner(data);
      c.nextLine();
      // start line
      c.nextLine().substring(1);
      String tmp = c.nextLine().substring(1);
      String premesse = "";
      for (int i = 1; i > 0;) {

         // System.out.println("^^^" + tmp);
         if (tmp.equalsIgnoreCase("#stop")) {
            descrizioneField.setText(premesse.substring(0, premesse.length() - 1));
            i = -1;
            break;
         }
         premesse = premesse + tmp + "\n";
         tmp = c.nextLine();
      }
      tempEsterno.setText(c.nextLine().substring(1));
      umidEsterno.setText(c.nextLine().substring(1));
      tempInterno.setText(c.nextLine().substring(1));
      umidInterno.setText(c.nextLine().substring(1));
      tempInterrato.setText(c.nextLine().substring(1));
      umidInterrato.setText(c.nextLine().substring(1));
      umAssEsterno.setText(c.nextLine().substring(1));
      umAssInterno.setText(c.nextLine().substring(1));
      umAssInterrato.setText(c.nextLine().substring(1));

      tmp = c.nextLine().substring(1);
      for (int i = 1; i > 0;) {
         String to = c.nextLine();
         if (to.equals("#meteoStop"))
            i = -1;
         else
            tmp = tmp + "\n" + to;

      }
      condizioniMeteo.setText(tmp);

      sourceIm1 = c.nextLine().substring(1);
      if (sourceIm1.contains("Premesse")) {
         pathIm1 = directoryProject.replace("aaAAzzZZ", st.getProject()) + "photo1Premesse." + "jpg";
         sourceIm1 = pathIm1;
         upload1.setIcon(new ImageIcon(pathIm1));
         photo.add(upload2, "cell 1 0");
      } else
         sourceIm1 = "";
      sourceIm2 = c.nextLine().substring(1);
      if (sourceIm2.contains("Premesse")) {
         pathIm2 = directoryProject.replace("aaAAzzZZ", st.getProject()) + "photo2Premesse." + "jpg";
         sourceIm2 = pathIm2;
         upload2.setIcon(new ImageIcon(pathIm2));
         photo.add(upload3, "cell 2 0");
      } else
         sourceIm2 = "";
      sourceIm3 = c.nextLine().substring(1);
      if (sourceIm3.contains("Premesse")) {

         pathIm3 = directoryProject.replace("aaAAzzZZ", st.getProject()) + "photo3Premesse." + "jpg";
         sourceIm3 = pathIm3;
         upload3.setIcon(new ImageIcon(pathIm3));
         photo.add(upload4, "cell 3 0");
      } else
         sourceIm3 = "";
      sourceIm4 = c.nextLine().substring(1);
      if (sourceIm4.contains("Premesse")) {

         pathIm4 = directoryProject.replace("aaAAzzZZ", st.getProject()) + "photo4Premesse." + "jpg";
         sourceIm4 = pathIm4;
         upload4.setIcon(new ImageIcon(pathIm4));
      } else
         sourceIm4 = "";
      paintAll(getGraphics());
   }

   public void resize(File in, int w, int h, String formato, String out) throws IOException {
      System.out.println(in + " - " + formato + " - " + out);
      Image newImg = ImageIO.read(in).getScaledInstance(w, h, Image.SCALE_SMOOTH);
      BufferedImage bim = new java.awt.image.BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      bim.createGraphics().drawImage(newImg, 0, 0, null);
      FileOutputStream fos = new FileOutputStream(out);
      ImageIO.write(bim, formato, fos);
      fos.close();
   }

   public boolean save() {
      boolean done = true;
      if (!sourceIm1.equals("") && !sourceIm1.equals(pathIm1))
         try {
            resize(new File(sourceIm1), 250, 200, "jpg", pathIm1.replace("aaAAzzZZ", st.getProject()));
         } catch (Exception e) {
            sourceIm1 = "";
            pathIm1 = "";
            upload1.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
            done = false;
            e.printStackTrace();
         }
      if (!sourceIm2.equals("") && !sourceIm2.equals(pathIm2))
         try {
            resize(new File(sourceIm2), 250, 200, "jpg", pathIm2.replace("aaAAzzZZ", st.getProject()));
         } catch (Exception e) {
            sourceIm2 = "";
            pathIm2 = "";
            upload2.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
            done = false;
            e.printStackTrace();
         }
      if (!sourceIm3.equals("") && !sourceIm3.equals(pathIm3))
         try {
            resize(new File(sourceIm3), 250, 200, "jpg", pathIm3.replace("aaAAzzZZ", st.getProject()));
         } catch (Exception e) {
            sourceIm3 = "";
            pathIm3 = "";
            upload3.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
            done = false;
            e.printStackTrace();
         }
      if (!sourceIm4.equals("") && !sourceIm4.equals(pathIm4))
         try {
            resize(new File(sourceIm4), 250, 200, "jpg", pathIm4.replace("aaAAzzZZ", st.getProject()));
         } catch (Exception e) {
            sourceIm4 = "";
            pathIm4 = "";
            upload4.setIcon(new ImageIcon(ClassLoader.getSystemResource("upload_icon_err.png")));
            done = false;
            e.printStackTrace();
         }

      return done;
   }

   public Document PDF(Document doc) {
      int col = 0;

      if (!sourceIm1.equals(""))
         col++;
      if (!sourceIm2.equals(""))
         col++;
      if (!sourceIm3.equals(""))
         col++;
      if (!sourceIm4.equals(""))
         col++;

      // foto
      PdfPTable table = new PdfPTable(2);
      table.setWidthPercentage(100);
      if (!pathIm1.equals(""))
         try {
            com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(pathIm1.replace("aaAAzzZZ", st.getProject()));
            img.setCompressionLevel(5);
            PdfPCell cos = new PdfPCell(img);
            if (col < 2) {
               img.scaleAbsolute(265f, 210f);
               cos.setColspan(2);
            } else
               img.scaleAbsolute(250f, 175f);
            cos.setHorizontalAlignment(Element.ALIGN_LEFT);
            cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cos.setBorder(PdfPCell.NO_BORDER);
            table.addCell(cos);
         } catch (BadElementException e) {
            e.printStackTrace();
         } catch (MalformedURLException e) {
            e.printStackTrace();
         } catch (IOException e) {
            e.printStackTrace();
         }
      if (!pathIm2.equals(""))
         try {
            com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(pathIm2.replace("aaAAzzZZ", st.getProject()));
            img.setCompressionLevel(5);
            if (col < 2) {
               img.scaleAbsolute(265f, 210f);
            } else
               img.scaleAbsolute(250f, 175f);
            PdfPCell cos = new PdfPCell(img);
            cos.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cos.setBorder(PdfPCell.NO_BORDER);
            table.addCell(cos);
         } catch (BadElementException e) {
            e.printStackTrace();
         } catch (MalformedURLException e) {
            e.printStackTrace();
         } catch (IOException e) {
            e.printStackTrace();
         }
      if (!pathIm3.equals(""))
         try {
            com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(pathIm3.replace("aaAAzzZZ", st.getProject()));
            img.setCompressionLevel(5);
            Paragraph dati;
            dati = new Paragraph("");
            PdfPCell space = new PdfPCell(dati);
            space.setBorder(PdfPCell.NO_BORDER);
            table.addCell(space);
            table.addCell(space);

            PdfPCell cos = new PdfPCell(img);
            if (col < 2) {
               img.scaleAbsolute(265f, 210f);
            } else
               img.scaleAbsolute(250f, 175f);
            if (col == 3)
               cos.setColspan(2);
            cos.setHorizontalAlignment(Element.ALIGN_LEFT);
            cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cos.setBorder(PdfPCell.NO_BORDER);
            table.addCell(cos);
         } catch (BadElementException e) {
            e.printStackTrace();
         } catch (MalformedURLException e) {
            e.printStackTrace();
         } catch (IOException e) {
            e.printStackTrace();
         }
      if (!pathIm4.equals(""))
         try {
            com.itextpdf.text.Image img = com.itextpdf.text.Image.getInstance(pathIm4.replace("aaAAzzZZ", st.getProject()));
            img.setCompressionLevel(5);
            PdfPCell cos = new PdfPCell(img);
            if (col < 2) {
               img.scaleAbsolute(265f, 210f);
            } else
               img.scaleAbsolute(250f, 175f);
            cos.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cos.setBorder(PdfPCell.NO_BORDER);
            table.addCell(cos);
         } catch (BadElementException e) {
            e.printStackTrace();
         } catch (MalformedURLException e) {
            e.printStackTrace();
         } catch (IOException e) {
            e.printStackTrace();
         }

      try {
         doc.add(table);
      } catch (DocumentException e) {
         e.printStackTrace();
      }

      // descrizione
      Paragraph dati;
      dati = new Paragraph(descrizioneField.getText(), FontFactory.getFont(FontFactory.TIMES, 11));
      try {
         dati.setSpacingBefore(10);
         if (col > 2)
            dati.setSpacingAfter(20);
         else
            dati.setSpacingAfter(120);
         doc.add(dati);
      } catch (DocumentException e2) {
         e2.printStackTrace();
      }

      // clima
      Chunk underline = new Chunk("2. Dati climatici ambientali", FontFactory.getFont(FontFactory.TIMES, 15, Font.BOLD));
      underline.setUnderline(1.2f, -2f); // 0.1 thick, -2 y-location

      Anchor anchorTarget = new Anchor(underline);
      anchorTarget.setName("ambiente");
      Paragraph targetParagraph = new Paragraph();
      targetParagraph.add(anchorTarget);
      try {
         targetParagraph.setSpacingAfter(10);
         doc.add(targetParagraph);
      } catch (DocumentException e2) {
         e2.printStackTrace();
      }

      table = new PdfPTable(4);
      table.setWidthPercentage(100);
      Phrase testo;
      PdfPCell cos;
      // line1
      testo = new Phrase("Clima ambientale", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(20);
      table.addCell(cos);

      testo = new Phrase("Temperatura", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(20);
      table.addCell(cos);

      testo = new Phrase("Umidità relativa", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(20);
      table.addCell(cos);

      testo = new Phrase("Umidità assoluta", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      cos.setFixedHeight(20);
      table.addCell(cos);

      // line2 esterno
      if (!tempEsterno.getText().equals("") && !umidEsterno.getText().equals("") && !umAssEsterno.getText().equals("   calcola")) {
         testo = new Phrase("Clima esterno", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);

         if (!tempEsterno.getText().equals(""))
            testo = new Phrase(tempEsterno.getText() + " °", FontFactory.getFont(FontFactory.TIMES, 11));
         else
            testo = new Phrase("", FontFactory.getFont(FontFactory.TIMES, 11));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);

         if (!umidEsterno.getText().equals(""))
            testo = new Phrase(umidEsterno.getText() + " %", FontFactory.getFont(FontFactory.TIMES, 11));
         else
            testo = new Phrase("", FontFactory.getFont(FontFactory.TIMES, 11));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);

         if (!umAssEsterno.getText().equals("   calcola"))
            testo = new Phrase(umAssEsterno.getText() + " g/m³", FontFactory.getFont(FontFactory.TIMES, 11));
         else
            testo = new Phrase("", FontFactory.getFont(FontFactory.TIMES, 11));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);
      }
      // line3 interrato
      if (!tempInterrato.getText().equals("") && !umidInterrato.getText().equals("") && !umAssInterrato.getText().equals("   calcola")) {
         testo = new Phrase("Clima interrato", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);
         if (!tempInterrato.getText().equals(""))
            testo = new Phrase(tempInterrato.getText() + " °", FontFactory.getFont(FontFactory.TIMES, 11));
         else
            testo = new Phrase("", FontFactory.getFont(FontFactory.TIMES, 11));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);
         if (!umidInterrato.getText().equals(""))
            testo = new Phrase(umidInterrato.getText() + " %", FontFactory.getFont(FontFactory.TIMES, 11));
         else
            testo = new Phrase("", FontFactory.getFont(FontFactory.TIMES, 11));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);
         if (!umAssInterrato.getText().equals("   calcola"))
            testo = new Phrase(umAssInterrato.getText() + " g/m³", FontFactory.getFont(FontFactory.TIMES, 11));
         else
            testo = new Phrase("", FontFactory.getFont(FontFactory.TIMES, 11));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);
      }
      // line4 interno
      if (!tempInterno.getText().equals("") && !umidInterno.getText().equals("") && !umAssInterno.getText().equals("   calcola")) {
         testo = new Phrase("Clima interno", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);
         if (!tempInterno.getText().equals(""))
            testo = new Phrase(tempInterno.getText() + " °", FontFactory.getFont(FontFactory.TIMES, 11));
         else
            testo = new Phrase("", FontFactory.getFont(FontFactory.TIMES, 11));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);
         if (!umidInterno.getText().equals(""))
            testo = new Phrase(umidInterno.getText() + " %", FontFactory.getFont(FontFactory.TIMES, 11));
         else
            testo = new Phrase("", FontFactory.getFont(FontFactory.TIMES, 11));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);
         if (!umAssInterno.getText().equals("   calcola"))
            testo = new Phrase(umAssInterno.getText() + " g/m³", FontFactory.getFont(FontFactory.TIMES, 11));
         else
            testo = new Phrase("", FontFactory.getFont(FontFactory.TIMES, 11));
         cos = new PdfPCell(testo);
         cos.setHorizontalAlignment(Element.ALIGN_CENTER);
         cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
         cos.setFixedHeight(20);
         table.addCell(cos);
      }
      // line4 meteo generico
      testo = new Phrase("Condizioni meteo al momento della diagnostica ", FontFactory.getFont(FontFactory.TIMES, 11, Font.BOLD));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      // cos.setFixedHeight(22);
      cos.setColspan(2);
      cos.disableBorderSide(PdfPCell.RIGHT);
      table.addCell(cos);

      testo = new Phrase(condizioniMeteo.getText(), FontFactory.getFont(FontFactory.TIMES, 11));
      cos = new PdfPCell(testo);
      cos.setHorizontalAlignment(Element.ALIGN_CENTER);
      cos.setVerticalAlignment(Element.ALIGN_MIDDLE);
      // cos.setFixedHeight(22);
      cos.disableBorderSide(PdfPCell.LEFT);
      cos.setColspan(2);
      table.addCell(cos);

      try {
         doc.add(table);
      } catch (DocumentException e2) {
         // TODO Auto-generated catch block
         e2.printStackTrace();
      }
      return doc;
   }

   public String formula(String temp, String umidita) {
      double f4 = Long.parseLong(temp);
      double g4 = Long.parseLong(umidita);

      double h4 = (Math.log10(g4 / 100) / 0.4343) + ((f4 * 17.62) / (f4 + 243.12));
      // System.out.println(g4/100);
      double i4 = (h4 * 243.12) / (17.62 - h4);
      // System.out.println(i4);
      double j4 = (17.62 * i4) / (243.12 + i4);
      // System.out.println(j4);
      double k4 = (6.112 * Math.exp(j4));
      // System.out.println(k4);

      return String.format("%.3g", k4 / (f4 + 273.15) * 216.4);
   }

   public void replace(String newName, String oldName) {
      sourceIm1 = pathIm1;
      sourceIm2 = pathIm2;
      sourceIm3 = pathIm3;
      sourceIm4 = pathIm4;
      pathIm1 = pathIm1.replace(oldName, "aaAAzzZZ");
      pathIm2 = pathIm2.replace(oldName, "aaAAzzZZ");
      pathIm3 = pathIm3.replace(oldName, "aaAAzzZZ");
      pathIm4 = pathIm4.replace(oldName, "aaAAzzZZ");
   }
}
